


/* Difference in difference on IV sample  */


use "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Revision\Data\panel_iv_long", replace

cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"

keep if time==1

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel.dta"

keep if _merge==3




sort pnr aar





/* Local macro for list of covariates */

local cov "yeardum2-yeardum40"





/* Local macro for selection of observations */


local selection "time>-5 & time<17 & sumpre>0 & sumpost>0"




/* Create covariates for regressions */



tab aar, gen(yeardum)											/* Year dummies */

tab time if (time>-5 & time<17), gen(timedum)					/* Time dummies */




/* Create variables for difference-in-difference setup */


gen treated=granted

gen after=0

replace after=1 if time>0

gen interaction=treated*after






/* IV Regress */

destring pnr, replace

xtset pnr aar



/**************************************************************/
/*                                                            */
/* MAIN REGRESSION WITH RELATIVE TIME TO APPLICATION DUMMIES  */
/*                                                            */
/**************************************************************/




/*****************/
/* Earned income */
/*****************/


gen outcome=erhvervsindk_real


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)



xtreg outcome interaction timedum2-timedum21 `cov' if (`selection'), cluster(pnr) fe

outreg2 using did_fixedeffects_IV.xls, replace ctitle(erhvervsindk_real)

outreg2 using did_fixedeffects_short_IV.xls, replace ctitle(erhvervsindk_real) keep(interaction)


drop outcome pre post sumpre sumpost



/******************************************/
/* Loop over remaining outcome variables  */
/******************************************/


local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social   mortgage_real bankgaeld_real"





foreach var of varlist `depvar'{



gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)



xtreg outcome interaction timedum2-timedum21 `cov' if (`selection'), cluster(pnr) fe

outreg2 using did_fixedeffects_IV.xls, append ctitle(`var')

outreg2 using did_fixedeffects_short_IV.xls, append ctitle(`var') keep(interaction)


drop outcome pre post sumpre sumpost




}

